package com.musemun.controller;

import com.alibaba.fastjson.JSON;
import com.musemun.common.CommonResult;
import com.musemun.common.ResultCode;
import com.musemun.common.ResultUtil;
import com.musemun.exception.ApiException;
import com.musemun.pojo.Crifo;
import com.musemun.pojo.Cropifo;
import com.musemun.pojo.Vo.CombineQueryVo;
import com.musemun.pojo.Vo.CropQueryVo;
import com.musemun.pojo.Vo.CropifoVo;
import com.musemun.service.function.CulturalRelic;
import com.musemun.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文物管理模块
 * </p>
 *
 * @author ZWYZY
 * @since 2020/4/27
 */
@RestController
@RequestMapping("/crms/crmsAdmin")
@Api(tags = "文物管理", description = "文物管理模块")
@Validated
public class CulturalRelicController {

    @Resource
    private CulturalRelic culturalRelic;

    //@Size(min = 1, max = 10,message = "超出范围")
    @GetMapping("/getCrifo/{crNo}")
    @ApiOperation(value = "id查询文物信息（小/pc）", httpMethod = "GET")
    @ApiImplicitParam(name = "crNo", value = "文物编号", required = true)
    public CommonResult getCrifo(@PathVariable Long crNo) {
        return ResultUtil.success(culturalRelic.getCrifo(crNo));
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增文物信息", httpMethod = "POST")
//    @ApiImplicitParam(name="crifo",value = "文物实体",required = true)
    public CommonResult insert(@RequestBody Crifo crifo) {
        return ResultUtil.success(culturalRelic.insert(crifo));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改文物信息（pc/小）", httpMethod = "PUT")
    public CommonResult update(@RequestBody Crifo crifo) {

        return ResultUtil.success(culturalRelic.update(crifo));
    }

    @DeleteMapping("/deleteByNo")
    @ApiOperation(value = "文物编号删除文物信息（pc/小）", httpMethod = "DELETE")
    @ApiImplicitParam(name = "crNo", value = "文物编号", required = true)
    public CommonResult deleteByNo(@RequestParam Long crNo) {
        return ResultUtil.success(culturalRelic.deleteByNo(crNo));
    }

    @DeleteMapping("/deleteByName")
    @ApiOperation(value = "文物名字删除文物信息（小）", httpMethod = "DELETE")
    @ApiImplicitParam(name = "crName", value = "文物名称", required = true)
    public CommonResult deleteByName(@RequestParam String crName) {
        return ResultUtil.success(culturalRelic.deleteByName(crName));
    }

    @DeleteMapping("/deleteByAddr")
    @ApiOperation(value = "文物收储位置删除文物信息（小）", httpMethod = "DELETE")
    @ApiImplicitParam(name = "crAddr", value = "文物收储地址", required = true)
    public CommonResult deleteByAddr(@RequestParam String crAddr) {
        return ResultUtil.success(culturalRelic.deleteByAddr(crAddr));
    }

    @GetMapping("/getListByStatus")
    @ApiOperation(value = "查询入馆/出馆/修复文物信息（小）", httpMethod = "GET")
    @ApiImplicitParam(name = "crState", value = "文物状态", required = true)
    public CommonResult getListByStatus(@RequestParam String crState) {
        return ResultUtil.success(culturalRelic.getListByStatus(crState));
    }

    @GetMapping("/getDownList")
    @ApiOperation(value = "获取下拉列表数组信息（pc）", httpMethod = "GET")
    public CommonResult getDownList() {
        return ResultUtil.success(culturalRelic.getDownList());
    }

    @GetMapping("/getUnList")
    @ApiOperation(value = "无条件分页查询文物信息（pc）", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数号"),
            @ApiImplicitParam(name = "pageSize", value = "页条数")})
    public CommonResult getUnList(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return ResultUtil.success(culturalRelic.getUnList(pageNum, pageSize));
    }

    @PostMapping("/getList")
    @ApiOperation(value = "有条件分页查询文物信息（pc）", httpMethod = "Post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数号"),
            @ApiImplicitParam(name = "pageSize", value = "页条数"),
//            @ApiImplicitParam(name = "combineQueryVo", value = "组合查询对象"),
            @ApiImplicitParam(name = "sort", value = "排序（默认为true）")})
    public CommonResult getList(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestBody(required = false) CombineQueryVo combineQueryVo,
            @RequestParam(required = false, defaultValue = "true") Boolean sort) {
        return ResultUtil.success(culturalRelic.getList(pageNum, pageSize, combineQueryVo, sort));
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询所有的文物信息（pc/小)", httpMethod = "GET")
    public CommonResult list() {
        return ResultUtil.success(culturalRelic.list());
    }

    @GetMapping("/selectCrop")
    @ApiOperation(value = "流动表查询所有信息（pc)", httpMethod = "GET")
    public CommonResult selectCrop() {
        return ResultUtil.success(culturalRelic.selectCrop());
    }

    @PostMapping("/selectCropByTime")
    @ApiOperation(value = "时间段+操作类型 查询流动表所有信息（pc）", httpMethod = "POST")
    public CommonResult selectCropByTime(@RequestBody CropQueryVo cropQueryVo) {
        return ResultUtil.success(culturalRelic.selectCropByTime(cropQueryVo));
    }
    @PostMapping("/deleteByCrop")
    @ApiOperation(value="删除流动记录（pc）",httpMethod = "POST")
    public CommonResult deleteByCrop(@RequestBody Cropifo cropifo)
    {
        return ResultUtil.success(culturalRelic.deleteByCrop(cropifo));
    }
    private static Logger log = LoggerFactory.getLogger(InController.class);

    @CrossOrigin(
            origins = "*",
            allowCredentials = "true",
            allowedHeaders = "*",
            methods = RequestMethod.GET,
            maxAge = 3600
    )
    @GetMapping("/exportExcel")
    @ApiOperation(value = "导出流动表（pc）", httpMethod = "GET")
    public void excelExport(HttpServletResponse response)  {
        log.info(() -> "文物解析开始");
        List<CropifoVo> c = culturalRelic.excelExport();
        try {
            ExcelUtil.ExcelDownload(c, response);
        } catch (IOException e) {
            throw new ApiException(ResultCode.EXCEL_NOT_DATA);
        }

        log.info(() -> "文物表下载成功");
//        return "草泥马";
    }
    @PutMapping("/updateStatus")
    @ApiOperation(value = "修改文物位置",httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "crWarehouse", value = "收储仓库", required = true),
            @ApiImplicitParam(name = "crAddr", value = "收储地点"),
            @ApiImplicitParam(name = "number", value = "0为入库，1为移动"),
            @ApiImplicitParam(name = "crNo", value = "文物编号", required = true)})
    public CommonResult updateStatus(@RequestParam Long crNo,@RequestParam String crWarehouse,@RequestParam String crAddr,@RequestParam int number){
        return ResultUtil.success(culturalRelic.updateStatus(crNo,crWarehouse,crAddr,number));
    }

}
